<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : !disabled ? '修改' : '查看'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
      <el-form-item label="网点名称" prop="name">
        <el-input v-model="dataForm.name" :disabled="disabled" placeholder="网点名称"></el-input>
      </el-form-item>
      <el-form-item label="查询经纬度" prop="longitude">
        <el-link href="https://lbs.qq.com/tool/getpoint/index.html" type="danger" :disabled="disabled" target="_blank">
          https://lbs.qq.com/tool/getpoint/index.html
        </el-link>
      </el-form-item><br>
      <el-form-item label="经纬度" prop="location">
        <el-input v-model="dataForm.location" :disabled="disabled" placeholder="经度"></el-input>
      </el-form-item>
      <el-form-item label="详细地址" prop="details">
        <el-input v-model="dataForm.details" :disabled="disabled" placeholder="详细地址"></el-input>
      </el-form-item>
      <el-form-item label="省市区" >
        <el-cascader-panel
          size="large"
          :disabled="disabled"
          :options="options"
          v-model="selectedOptions"
          @change="handleChange">
        </el-cascader-panel>
      </el-form-item>
      <el-form-item label="营业时间" prop="workTime">
        <el-time-picker
          is-range
          value-format="H:mm"
          :disabled="disabled"
          v-model="dataForm.workTime"
          range-separator="至"
          start-placeholder="开始时间"
          end-placeholder="结束时间"
          placeholder="营业时间">
        </el-time-picker>
      </el-form-item>
      <el-form-item label="电话号码" prop="telephone">
        <el-input v-model="dataForm.telephone" :disabled="disabled" placeholder="电话号码"></el-input>
      </el-form-item>
      <el-form-item label="网点图片" prop="imgUrl">
        <el-img v-model="dataForm.imgUrl" :disabled="disabled"></el-img>
      </el-form-item>
      <el-form-item label="网点层级" prop="hierarchy">
        <el-select v-model="dataForm.hierarchy" :disabled="disabled" clearable filterable placeholder="请选择" class="width185">
          <el-option
            v-for="item in [{value:1,label:'一级'},{value:2,label:'二级'},{value:3,label:'三级'}]"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="上级网点" prop="parentId">
        <el-input v-model="dataForm.parentId" :disabled="disabled" placeholder="上级网点ID"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="desc">
        <ueditor v-model="dataForm.shopDesc" :disabled="disabled" placeholder="描述"></ueditor>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button v-if="!disabled" type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import {CodeToText, regionData, TextToCode} from "element-china-area-data";

export default {
  data() {
    let locationRule = {
      validator: (rule, value, callback) => {
        if (value && value !== '') {
          let temp = value.split(',')
          if (temp.length == 2) {
            // 经纬度必须在固定值内
            if (Math.abs(temp[1] - 60) < 60 && Math.abs(temp[0] - 50) < 50) {
              callback();
            }
          }
            callback(new Error('仅支持境内。请参考：28.236008,113.086002'));
          }
          callback();
        },
        trigger: 'blur'
      }
      return {
        disabled: false,
        visible: false,
        options: regionData,
        selectedOptions: [],
        dataForm: {
          id: 0,
          name: '',
          location: '',
          details: '',
          province: '',
          city: '',
          district: '',
          workTime: [new Date(), new Date()],
          telephone: '',
          imgUrl: '',
          hierarchy: '',
          desc: '',
          parentId: '',
          createUserId: '',
          createTime: ''},
        dataRule: {
          // location: [locationRule],
          name: [
            {required: true, message: '名称不能为空', trigger: 'blur'}
          ]
        }
      }
    },
    methods: {
      init (id, disabled) {
        this.disabled = disabled
        this.dataForm.id = id || ''
        this.visible = true
        this.selectedOptions = []
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: `/mall/branch/info/${this.dataForm.id}`,
              method: 'get'
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm = data.branch
                this.selectedOptions.push(TextToCode[this.dataForm.province].code)
                this.selectedOptions.push(TextToCode[this.dataForm.province][this.dataForm.city].code)
                this.selectedOptions.push(TextToCode[this.dataForm.province][this.dataForm.city][this.dataForm.district].code)
                if(this.dataForm.workTime){
                  this.dataForm.workTime = this.dataForm.workTime.splice("-")
                }
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.dataForm.location = this.dataForm.location.replace(","," ")
        this.$refs['dataForm']
          .validate((valid) => {
            if (valid) {
              this.dataForm.workTime = this.dataForm.workTime?this.dataForm.workTime[0]+"-"+this.dataForm.workTime[1]:''
              this.$http({
                url: `/mall/branch/${!this.dataForm.id ? 'save' : 'update'}`,
                method: 'post',
                data: this.dataForm
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500
                  })
                  this.visible = false
                  this.$emit('refreshDataList')
                }
              })
            }
          })
      },
      handleChange (value) {
        this.dataForm.province = CodeToText[value[0]]
        this.dataForm.city = CodeToText[value[1]]
        this.dataForm.district = CodeToText[value[2]]
      }
    }
  }
</script>
